/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ds_Stack;

import java.util.Arrays;
/**
 *
 * @author hcc25_000
 */
public class MyArrayStack<T> implements MyStackADT<T> {
    private final int DEFAULT_CAPACITY=100;
    
    private int top;
    private T[] stack;
    
    public MyArrayStack(){
        top=0;
        stack= (T[]) (new Object[DEFAULT_CAPACITY]);
    }
    
    public MyArrayStack(int initalCapacity){
        top=0;
        stack=(T[]) (new Object[initalCapacity]);
    }
    
    private void expandCapacity(){
        stack=Arrays.copyOf(stack,stack.length*2);
    }
    
    public void push (T element){
        if(size()==stack.length)
            expandCapacity();
        stack[top++]=element;
    }
    
    public T pop(){
        if(isEmpty())
            throw new MyEmptyCollectionException("Stack");
        T result=stack[--top];
        stack[top]=null;
        
        return result;
    }
    
    public T peek(){
        if(isEmpty())
            throw new MyEmptyCollectionException("Stack");
        
        return stack[top-1];
    }
    
    public boolean isEmpty(){
        return top==0;
    }
    
    public int size(){
        return top;
    }
    
    public String toString(){
        return new String("MyArrayStack");
    }
    
    public void showInfo(){
        if(isEmpty())
            throw new MyEmptyCollectionException("Stack");
        
        for(int sign=0;sign<top;sign++)
            System.out.println("The "+(sign+1)+" number"+" is "+stack[sign]);
    }
}


